{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "样本类内离散度矩阵S1： [[5247.5 9.8999999999998 -0.24999999999982836 9.1]\n",
      " [9.8999999999998 4.031480000000004 2.9498400000000005 0.5271200000000003]\n",
      " [-0.24999999999982836 2.9498400000000005 3.585719999999999\n",
      "  0.05895999999999962]\n",
      " [9.1 0.5271200000000003 0.05895999999999962 1.00128]] \n",
      "\n",
      "样本类内离散度矩阵S2： [[5247.5 -31.85000000000003 -11.649999999999858 -16.700000000000074]\n",
      " [-31.85000000000003 9.181679999999998 3.0413999999999977\n",
      "  5.8948000000000045]\n",
      " [-11.649999999999858 3.0413999999999977 3.1389999999999993 2.514]\n",
      " [-16.700000000000074 5.8948000000000045 2.514 6.288000000000002]] \n",
      "\n",
      "样本类内离散度矩阵S3： [[5247.5 19.849999999999504 11.799999999999926 -32.35000000000008]\n",
      " [19.849999999999504 13.762319999999999 3.172359999999998\n",
      "  11.374480000000005]\n",
      " [11.799999999999926 3.172359999999998 3.2962799999999994\n",
      "  2.4840399999999994]\n",
      " [-32.35000000000008 11.374480000000005 2.4840399999999994\n",
      "  11.468720000000005]] \n",
      "\n",
      "-----------------------------------------------------------------------------------------------\n",
      "总体类内离散度矩阵Sw12： [[ 1.049500e+04 -2.195000e+01 -1.190000e+01 -7.600000e+00]\n",
      " [-2.195000e+01  1.321316e+01  5.991240e+00  6.421920e+00]\n",
      " [-1.190000e+01  5.991240e+00  6.724720e+00  2.572960e+00]\n",
      " [-7.600000e+00  6.421920e+00  2.572960e+00  7.289280e+00]] \n",
      "\n",
      "总体类内离散度矩阵Sw13： [[ 1.04950e+04  2.97500e+01  1.15500e+01 -2.32500e+01]\n",
      " [ 2.97500e+01  1.77938e+01  6.12220e+00  1.19016e+01]\n",
      " [ 1.15500e+01  6.12220e+00  6.88200e+00  2.54300e+00]\n",
      " [-2.32500e+01  1.19016e+01  2.54300e+00  1.24700e+01]] \n",
      "\n",
      "总体类内离散度矩阵Sw23： [[ 1.049500e+04 -1.200000e+01  1.500000e-01 -4.905000e+01]\n",
      " [-1.200000e+01  2.294400e+01  6.213760e+00  1.726928e+01]\n",
      " [ 1.500000e-01  6.213760e+00  6.435280e+00  4.998040e+00]\n",
      " [-4.905000e+01  1.726928e+01  4.998040e+00  1.775672e+01]] \n",
      "\n",
      "-----------------------------------------------------------------------------------------------\n",
      "判断出来的综合正确率： 96.66666666666667 %\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt \n",
    "import seaborn as sns\n",
    "path=r'Iris.csv'\n",
    "df = pd.read_csv(path, header=0)\n",
    "Iris1=df.values[0:50,0:4]\n",
    "Iris2=df.values[50:100,0:4]\n",
    "Iris3=df.values[100:150,0:4]\n",
    "m1=np.mean(Iris1,axis=0)\n",
    "m2=np.mean(Iris2,axis=0)\n",
    "m3=np.mean(Iris3,axis=0)\n",
    "s1=np.zeros((4,4))\n",
    "s2=np.zeros((4,4))\n",
    "s3=np.zeros((4,4))\n",
    "for i in range(0,30,1):\n",
    "    a=Iris1[i,:]-m1\n",
    "    a=np.array([a])\n",
    "    b=a.T\n",
    "    s1=s1+np.dot(b,a)    \n",
    "for i in range(0,30,1):\n",
    "    c=Iris2[i,:]-m2\n",
    "    c=np.array([c])\n",
    "    d=c.T\n",
    "    s2=s2+np.dot(d,c) \n",
    "    #s2=s2+np.dot((Iris2[i,:]-m2).T,(Iris2[i,:]-m2))\n",
    "for i in range(0,30,1):\n",
    "    a=Iris3[i,:]-m3\n",
    "    a=np.array([a])\n",
    "    b=a.T\n",
    "    s3=s3+np.dot(b,a) \n",
    "sw12=s1+s2\n",
    "sw13=s1+s3\n",
    "sw23=s2+s3\n",
    "#投影方向\n",
    "a=np.array([m1-m2])\n",
    "sw12=np.array(sw12,dtype='float')\n",
    "sw13=np.array(sw13,dtype='float')\n",
    "sw23=np.array(sw23,dtype='float')\n",
    "#判别函数以及T\n",
    "#需要先将m1-m2转化成矩阵才能进行求其转置矩阵\n",
    "a=m1-m2\n",
    "a=np.array([a])\n",
    "a=a.T\n",
    "b=m1-m3\n",
    "b=np.array([b])\n",
    "b=b.T\n",
    "c=m2-m3\n",
    "c=np.array([c])\n",
    "c=c.T\n",
    "w12=(np.dot(np.linalg.inv(sw12),a)).T\n",
    "w13=(np.dot(np.linalg.inv(sw13),b)).T\n",
    "w23=(np.dot(np.linalg.inv(sw23),c)).T\n",
    "#print(m1+m2) #1x4维度  invsw12 4x4维度  m1-m2 4x1维度\n",
    "T12=-0.5*(np.dot(np.dot((m1+m2),np.linalg.inv(sw12)),a))\n",
    "T13=-0.5*(np.dot(np.dot((m1+m3),np.linalg.inv(sw13)),b))\n",
    "T23=-0.5*(np.dot(np.dot((m2+m3),np.linalg.inv(sw23)),c))\n",
    "kind1=0\n",
    "kind2=0\n",
    "kind3=0\n",
    "newiris1=[]\n",
    "newiris2=[]\n",
    "newiris3=[]\n",
    "for i in range(30,49):\n",
    "    x=Iris1[i,:]\n",
    "    x=np.array([x])\n",
    "    g12=np.dot(w12,x.T)+T12\n",
    "    g13=np.dot(w13,x.T)+T13\n",
    "    g23=np.dot(w23,x.T)+T23\n",
    "    if g12>0 and g13>0:\n",
    "        newiris1.extend(x)\n",
    "        kind1=kind1+1\n",
    "    elif g12<0 and g23>0:\n",
    "        newiris2.extend(x)\n",
    "    elif g13<0 and g23<0 :\n",
    "        newiris3.extend(x)\n",
    "#print(newiris1)\n",
    "for i in range(30,49):\n",
    "    x=Iris2[i,:]\n",
    "    x=np.array([x])\n",
    "    g12=np.dot(w12,x.T)+T12\n",
    "    g13=np.dot(w13,x.T)+T13\n",
    "    g23=np.dot(w23,x.T)+T23\n",
    "    if g12>0 and g13>0:\n",
    "        newiris1.extend(x)\n",
    "    elif g12<0 and g23>0:\n",
    "       \n",
    "        newiris2.extend(x)\n",
    "        kind2=kind2+1\n",
    "    elif g13<0 and g23<0 :\n",
    "        newiris3.extend(x)\n",
    "for i in range(30,50):\n",
    "    x=Iris3[i,:]\n",
    "    x=np.array([x])\n",
    "    g12=np.dot(w12,x.T)+T12\n",
    "    g13=np.dot(w13,x.T)+T13\n",
    "    g23=np.dot(w23,x.T)+T23\n",
    "    if g12>0 and g13>0:\n",
    "        newiris1.extend(x)\n",
    "    elif g12<0 and g23>0:     \n",
    "        newiris2.extend(x)\n",
    "    elif g13<0 and g23<0 :\n",
    "        newiris3.extend(x)\n",
    "        kind3=kind3+1\n",
    "correct=(kind1+kind2+kind3)/60\n",
    "print(\"样本类内离散度矩阵S1：\",s1,'\\n')\n",
    "print(\"样本类内离散度矩阵S2：\",s2,'\\n')\n",
    "print(\"样本类内离散度矩阵S3：\",s3,'\\n')\n",
    "print('-----------------------------------------------------------------------------------------------')\n",
    "print(\"总体类内离散度矩阵Sw12：\",sw12,'\\n')\n",
    "print(\"总体类内离散度矩阵Sw13：\",sw13,'\\n')\n",
    "print(\"总体类内离散度矩阵Sw23：\",sw23,'\\n')\n",
    "print('-----------------------------------------------------------------------------------------------')\n",
    "print('判断出来的综合正确率：',correct*100,'%')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
